package com.quyunshuo.module.home.fragment.home

import android.util.Log
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.quyunshuo.edwinpro.common.ui.BaseFragment
import com.quyunshuo.edwinpro.network.bean.ProjectTabItem
import com.quyunshuo.module.home.activity.concentration.ConcentrationActivity
import com.quyunshuo.module.home.activity.time.TimeActivity
import com.quyunshuo.module.home.databinding.HomeFragmentHomeBinding
import com.quyunshuo.module.home.dialog.ConcentrationDialog
import com.quyunshuo.module.home.dialog.SleepDialog
import com.quyunshuo.module.home.dialog.TestDialog
import com.quyunshuo.module.home.manager.TimeManager
import com.quyunshuo.module.home.room.database.SumDataBase
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import kotlin.math.min

/**
 * @author DBoy 2021/7/6 <p>
 * - 文件描述 : 测试fragment
 */
@AndroidEntryPoint
class HomeFragment : BaseFragment<HomeFragmentHomeBinding, HomeViewModel>() {
    private val TAG = "HomeFragment"
    val timeManager = TimeManager()
    override val mViewModel by viewModels<HomeViewModel>()
    override fun createVB() = HomeFragmentHomeBinding.inflate(layoutInflater)

    override fun HomeFragmentHomeBinding.initListener() {
        concentration.setOnClickListener {
            ConcentrationDialog.newInstance().apply {
                setCallback {
                    //进行页面跳转
                    ConcentrationActivity.newInstance(requireContext(),it.toLong())
                }

            }.showAllowingStateLoss(getParentFragmentManager(), "")
        }
        sleep.setOnClickListener {
            SleepDialog.newInstance().apply {
                setCallback {hour,minute->
                    val minutesUntilTarget = timeManager.getMinutesUntilTarget(hour, minute)
                    Log.d(TAG, "initListener: $minutesUntilTarget")
                    //进行页面跳转
                    ConcentrationActivity.newInstance(requireContext(),minutesUntilTarget)
//                    // 判断目标时间是今天还是明天
//                    val targetTime = timeManager.getTargetDateTime(hour, minute)
//                    val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
//                    Log.d(TAG, "目标时间将在: ${formatter.format(targetTime.time)} 触发")
//
//                    // 启动时间检查
//                    timeManager.startTimeComparison(hour, minute) { reached ->
//                        if (reached) {
//                            // 当达到12:58时执行的操作
//                            Log.d(TAG, "触发成功！现在是12:58")
//                            // 执行你的响应逻辑，例如显示通知、播放声音等
//                        } else {
//                            // 未到达时的处理（可选）
//                            val now = Calendar.getInstance()
//                            val diff = (targetTime.timeInMillis - now.timeInMillis) / 1000
//                            Log.d(TAG, "倒计时: ${diff / 60}分${diff % 60}秒")
//                        }
//                    }
                }

            }.showAllowingStateLoss(getParentFragmentManager(), "")
        }

        breathe.setOnClickListener {
            TimeActivity.newInstance(requireContext())
        }
    }

    private var mProjectTabs = mutableListOf<ProjectTabItem>()

    override fun HomeFragmentHomeBinding.initView() {
        time.text = mViewModel.getLocalizedTimePeriod(requireContext())

    }

    override fun onDestroyView() {
        super.onDestroyView()
    }

    override fun initObserve() {
//        observeLiveData(mViewModel.firstData,::firstData)
        lifecycleScope.launch {
        }
    }

    private fun firstData(projectTabItems: MutableList<ProjectTabItem>?) {
        //拿到数据了，那么就要给adapter设置了。
        projectTabItems?.forEachIndexed { index, item ->
            mProjectTabs.add(item)
        }
    }

    override fun initRequestData() {
        mViewModel.getData()
    }


}